#include "ssbase/base_cfg.h"
#include <Windows.h>
#include <tchar.h>
#include "ssui/ui_export.h"
#include "ssui_render/ui_render_export.h"
_SSUIL_USE_NP


class MyMessageFilter :
	public IUIMessageFilterInterface,
	virtual public ref_counted_base
{
public:
	virtual LRESULT MessageHandle(IUIManagerInterface *manager, UINT msg, WPARAM wparam, LPARAM lparam, bool &handled)
	{
		switch(msg)
		{
		case WM_RBUTTONDOWN:
		case WM_NCRBUTTONDOWN:
			{
				scoped_ref_ptr<IUIControlInterface> ani_control = manager->GetContent()->FindControl(_T("ani_test"));
				if(ani_control != NULL)
				{
					scoped_ref_ptr<IUIControlInternalAnimationInterface> animation;
					animation.assign_no_ref(new UIControlInternalTransformAnimation(UIPoint(0, 0), UIPoint(400, 200)));
					ani_control->DoAnimation(animation);
					animation.assign_no_ref(new UIControlInternalSizeAnimation(UISize(50, 50), UISize(200,100)));
					ani_control->DoAnimation(animation);
				}
			}
			break;
		}
		return 0L;
	}
};

void PrintSliderValue(UISlider *slider)
{
	if(slider == NULL)
		return;
	tstring lbl_name = slider->GetName();
	lbl_name += _T("_value");
	UILabel *lbl = dynamic_cast<UILabel*>(slider->GetWindowManager()->GetContent()->FindControl(lbl_name).get());
	if(lbl == NULL)
		return;
	lbl->SetText(auto_cvt::from_fmt(_T("value:%d"), slider->GetValue()).c_tstr());
}

bool OnSliderValueChangedCB(void *tag)
{
	UISlider *slider = dynamic_cast<UISlider*>(static_cast<IUIControlInterface*>(tag));
	PrintSliderValue(slider);
	return false;
}

bool OnNewWindowButtonClicked(void *tag)
{
	IUIControlInterface *control = static_cast<IUIControlInterface*>(tag);
	SS_ASSERT(control != NULL);

	scoped_ref_ptr<IUIManagerInterface> win_man = control->GetWindowManager();
	SS_ASSERT(win_man != NULL);

	scoped_ref_ptr<IUIBuilderInterface> builder = win_man->GetBuilder();
	SS_ASSERT(builder != NULL);

	scoped_ref_ptr<IUIResourceRenderInterface> rr = win_man->GetResourceRener();
	SS_ASSERT(rr != NULL);

	scoped_ref_ptr<IUIDiskFilesManagerInterface> dfm = win_man->GetDiskFilesManager();
	SS_ASSERT(dfm != NULL);

	scoped_ref_ptr<IUIWindowBgCanvasInterface> bg_canvas = win_man->GetBackgroundBgCanvas();
	SS_ASSERT(bg_canvas != NULL);

	scoped_ref_ptr<IUIWindow> ui_win;
	builder->Build(dfm, _T("skin.xml"), control->GetName().c_str(), true,
		bg_canvas->New(), rr, ui_win , NULL, 0, win_man->GetInstance());
	if(ui_win == NULL)
		return false;

	scoped_ref_ptr<IUIMessageFilterInterface> msg_filter;
	msg_filter.assign_no_ref(new MyMessageFilter);
	ui_win->GetWindowManager()->AppendMessageFilter(msg_filter);

	{
		scoped_ref_ptr<UIImageViewer> img_viewer = dynamic_cast<UIImageViewer*>(
			ui_win->GetWindowManager()->GetContent()->FindControl(_T("img_viewer")).get());
		if(img_viewer != NULL)
			img_viewer->BeginView();
	}

	ui_win->ShowWindowEx(true, true, true, false);
	ui_win->SetReleaseOnDestoryMessage(true);

	return true;
}

int APIENTRY _tWinMain(HINSTANCE hInstance,
	HINSTANCE hPrevInstance,
	LPTSTR    lpCmdLine,
	int       nCmdShow)
{
	UIRenderInit();

	{
		scoped_ref_ptr<IUIBuilderInterface> builder;
		builder.assign_no_ref(new UIBuilder());

		scoped_ref_ptr<IUIDiskFilesManagerInterface> dfm;
		dfm.assign_no_ref(new UIDiskFilesManager(0));
		dynamic_cast<UIDiskFilesManager*>(dfm.get())->SetBasePath(
			_T("./skins"));

		scoped_ref_ptr<IUIWindowBgCanvasInterface> bg_canvas;
		bg_canvas.assign_no_ref(new UIBackgroundCanvas);

		scoped_ref_ptr<IUIResourceRenderInterface> rr;
		rr.assign_no_ref(new UIResourceRender);

		scoped_ref_ptr<IUIWindow> ui_win;
		builder->Build(dfm, _T("skin.xml"), _T("main_wnd"), true,
			bg_canvas, rr, ui_win , NULL, 0, hInstance);

		{
			scoped_ref_ptr<UISlider> slider = dynamic_cast<UISlider*>(
				ui_win->GetWindowManager()->GetContent()->FindControl(_T("horz_slider")).get());
			if(slider != NULL)
			{
				slider->OnValueChanged += OnSliderValueChangedCB;
				OnSliderValueChangedCB((void*)slider);
			}

			slider = dynamic_cast<UISlider*>(
				ui_win->GetWindowManager()->GetContent()->FindControl(_T("horz_slider_udconvert")).get());
			if(slider != NULL)
			{
				slider->OnValueChanged += OnSliderValueChangedCB;
				OnSliderValueChangedCB((void*)slider);
			}

			slider = dynamic_cast<UISlider*>(
				ui_win->GetWindowManager()->GetContent()->FindControl(_T("vert_slider")).get());
			if(slider != NULL)
			{
				slider->OnValueChanged += OnSliderValueChangedCB;
				OnSliderValueChangedCB((void*)slider);
			}

			slider = dynamic_cast<UISlider*>(
				ui_win->GetWindowManager()->GetContent()->FindControl(_T("vert_slider_udconvert")).get());
			if(slider != NULL)
			{
				slider->OnValueChanged += OnSliderValueChangedCB;
				OnSliderValueChangedCB((void*)slider);
			}
		}

		{
			scoped_ref_ptr<UIButton> but = dynamic_cast<UIButton*>(
				ui_win->GetWindowManager()->GetContent()->FindControl(_T("window2")).get());
			but->OnClick += OnNewWindowButtonClicked;

			but = dynamic_cast<UIButton*>(
				ui_win->GetWindowManager()->GetContent()->FindControl(_T("old_main_wnd")).get());
			but->OnClick += OnNewWindowButtonClicked;
		}
		ui_win->ShowWindowEx(true, true, true, false);

		MSG msg;
		while(::GetMessage(&msg, NULL, 0, 0))
		{
			::TranslateMessage(&msg);
			::DispatchMessage(&msg);
		}
	}

	UIRenderUninit();
	return 0;
}

